﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;

namespace YJKIndicateDimension.DrawLaneLine
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class ChangeToLaneLines : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			this.m_app = commandData.Application;
			this.m_uidoc = this.m_app.ActiveUIDocument;
			this.m_doc = this.m_uidoc.Document;
			this.m_sel = this.m_uidoc.Selection;
			try
			{
				DetailCurveFilter detailCurveFilter = new DetailCurveFilter();
				IList<Reference> list = this.m_sel.PickObjects((Autodesk.Revit.UI.Selection.ObjectType)1, detailCurveFilter, "请框选需要转换为车道线的详图线");
				Transaction transaction = new Transaction(this.m_doc, "ChangeCurve");
				try
				{
					transaction.Start();
					foreach (Reference reference in list)
					{
						LaneLineXDataManager.WriteXData(this.m_doc.GetElementByRef(reference), "车道线");
					}
					transaction.Commit();
				}
				catch (Exception)
				{
					transaction.RollBack();
				}
			}
			catch (Exception)
			{
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private void ChangeDetailCurve(XYZ picPt, ref DetailCurve detailCurve)
		{
			Transaction transaction = new Transaction(this.m_doc, "changeCurve");
			try
			{
				transaction.Start();
				LocationCurve locationCurve = detailCurve.Location as LocationCurve;
				Curve curve = locationCurve.Curve;
				XYZ hyendPoint = curve.GetEndPoint(0);
				XYZ hyendPoint2 = curve.GetEndPoint(1);
				if (hyendPoint.DistanceTo(picPt) > hyendPoint2.DistanceTo(picPt))
				{
					if (curve is Arc)
					{
						Arc arc = curve as Arc;
						Arc curve2 = AppDrawLaneLines.CreateArc(this.m_doc, arc.Center, hyendPoint2, hyendPoint);
						locationCurve.Curve = curve2;
					}
					else
					{
						Curve curve3 = YJKLineEx.YJKGetBound(hyendPoint2, hyendPoint);
						locationCurve.Curve = curve3;
					}
				}
				Parameter parameter = detailCurve.GetParameter("实例用途");
				if (parameter == null)
				{
					string pathName = Path.Combine(Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)), "Data"), "HYShared Parameters.txt");
					YJKRevitTools.SetInstanceParameter(this.m_app, pathName, "详图线布置", "实例用途", BuiltInCategory.OST_Lines, true);
					parameter = detailCurve.GetParameter("实例用途");
				}
				parameter.Set("车道线");
				transaction.Commit();
			}
			catch (Exception)
			{
				transaction.RollBack();
			}
		}

		private UIApplication m_app;

		private UIDocument m_uidoc;

		private Document m_doc;

		private Selection m_sel;
	}
}
